package game;

import game.gfx.ImageReader;
import game.gfx.SpriteSheet;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;


public class GameMain extends Canvas implements Runnable
{	
	private static final long serialVersionUID = 1L;
	
	public static final int WIDTH = 160;
	public static int HEIGHT = WIDTH / 12 * 9;
	public static final int SCALE = 4;
	public static final String NAME = "Game Engine 1";
	
	private JFrame frame;
	
	public boolean running = false;
	public int tickCount = 0;
	
	private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

	public GameMain()
	{
		setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
		setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
		setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
		
		frame = new JFrame(NAME);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());
		
		frame.add(this, BorderLayout.CENTER);
		frame.pack();
		
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	public void init()
	{
		
	}
	
	public void start()
	{
		running = true;
		new Thread(this).start();
	}
	public void stop()
	{
		running = false;
	}
	
	public void run()
	{
		long lastTime = System.nanoTime();
		double nsPerTick = 1000000000D / 60D;
		
		int ticks = 0;
		int frames = 0;
		
		long lastTimer = System.currentTimeMillis();
		double delta = 0;
		
		while(running)
		{
			long now = System.nanoTime();
			delta += (now - lastTime) / nsPerTick;
			lastTime = now;
			
			boolean shouldRender = true;
			
			while(delta >= 1)
			{
				ticks++;
				tick();
				delta -= 1;		
				shouldRender = true;
			}
			
			try
			{
				Thread.sleep(2);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			if (shouldRender)
			{
				frames++;
				render();
			}
			
			if (System.currentTimeMillis() - lastTimer >= 1000)
			{
				lastTimer += 1000;
				frame.setTitle(ticks + " ticks, " + frames + " frames.");
				frames = 0;
				ticks = 0;
			}
		}
	}
	
	private void tick()
	{
		tickCount++;
	}
	
	private void render()
	{
		BufferStrategy bs = getBufferStrategy();
		if (bs == null)
		{
			createBufferStrategy(3);
			return;
		}
		
		
		
//		Graphics g = bs.getDrawGraphics();
//		
//		g.setColor(Color.BLACK);
//		g.fillRect(0, 0, getWidth(), getHeight());
//		
//		g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
//		
//		g.dispose();
//		bs.show();
	}

	public static void main(String[] args)
	{
		new GameMain().start();
		
		
		ImageReader imageReader = new ImageReader("/res/sprite_sheet.png");
		SpriteSheet sheet = new SpriteSheet(imageReader);
		
		String[][] tileData = sheet.getTileData(2, 0);
		
		for (int y = 0; y < 8; y++)
		{
			for (int x = 0; x < 8; x++)
			{
				System.out.println(x + " " + y + " " + tileData[x][y]);				
			}
			System.out.println("\n");
		}
	}
}
